package com.zhou.syt.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;

import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import com.zhou.syt.hosp.repository.DepartmentRespotitory;
import com.zhou.syt.hosp.service.DepartmentService;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Driver;
import java.util.Date;
import java.util.Map;

/**
 * @ClassName: DepartmentServiceImpl
 * @Description:
 * @Author: admin
 * @Date: 2022/1/8 21:04
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Resource
    DepartmentRespotitory departmentRespotitory;

    @Override
    public void save(Map<String, Object> stringObjectMap) {
        //把参数转为department对象
        String s = JSONObject.toJSONString(stringObjectMap);
        Department department = JSONObject.parseObject(s, Department.class);

        Department departmentExist = departmentRespotitory.getDepartmentByHoscodeAndDepcode(department.getHoscode() ,department.getDepcode() );

        if (departmentExist != null) {
            departmentExist.setUpdateTime(new Date());
            departmentExist.setIsDeleted(0);
            departmentRespotitory.save(departmentExist);
        } else {
            department.setCreateTime(new Date());
            department.setIsDeleted(0);
            department.setUpdateTime(new Date());
            departmentRespotitory.save(department);
        }
    }

    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {

        //创建Pageable对象，设置当前页和每页记录数
        Pageable pageable = PageRequest.of(page - 1, limit);
        //创建Example对象

        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        department.setIsDeleted(0);
        ExampleMatcher matcher = ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING).withIgnoreCase(true);
        Example<Department> example = Example.of(department, matcher);

        Page<Department> all=departmentRespotitory.findAll(example,pageable);
        return all;
    }

    @Override
    public void remove(String hoscode, String depcode) {
        Department department = departmentRespotitory.getDepartmentByHoscodeAndDepcode(hoscode, depcode);

        if (department != null) {
            department.setIsDeleted(1);
            departmentRespotitory.save(department);
        }
    }


}
